<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Migrating from libunique to GApplication or GtkApplication</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="GTK+ 3 参考手册">
<link rel="up" href="migrating.html" title="Part V. 从GTK+的旧版本迁移程序">
<link rel="prev" href="ch25s02.html" title="Changes that need to be done at the time of the switch">
<link rel="next" href="ch26s02.html" title="Commands and Messages">
<meta name="generator" content="GTK-Doc V1.18 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="ch25s02.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="migrating.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GTK+ 3 参考手册</th>
<td><a accesskey="n" href="ch26s02.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="chapter">
<div class="titlepage"><div><div><h2 class="title">
<a name="gtk-migrating-GtkApplication"></a>Migrating from libunique to GApplication or GtkApplication</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="gtk-migrating-GtkApplication.html#id3296490">Uniqueness</a></span></dt>
<dt><span class="section"><a href="ch26s02.html">Commands and Messages</a></span></dt>
</dl></div>
<p>
    libunique offers 'unique application' support as well as ways to
    communicate with a running application instance. This is implemented
    in various ways, either using D-Bus, or socket-based communication.
  </p>
<p>
    Starting with GLib 2.26, D-Bus support has been integrated into GIO
    in the form of GDBus, and #GApplication has been added to provide
    the same level of application support as libunique.
  </p>
<div class="example">
<a name="id3637263"></a><p class="title"><b>Example 98. A unique application</b></p>
<div class="example-contents">
<p>Here is a simple application using libunique:
  </p>
<pre class="programlisting">
int
main (int argc, char *argv[])
{
  UniqueApp *app;
  GtkWidget *window;

  gtk_init (&amp;argc, &amp;argv);

  app = unique_app_new ("org.gtk.TestApplication", NULL);

  if (unique_app_is_running (app))
    {
      UniqueResponse response;

      response = unique_app_send_message (app, UNIQUE_ACTIVATE, NULL);
      g_object_unref (app);

      return response == UNIQUE_RESPONSE_OK ? 0 : 1;
    }

  window = create_my_window ();

  unique_app_watch_window (app, GTK_WINDOW (window));

  gtk_widget_show (window);

  gtk_main ();

  g_object_unref (app);

  return 0;
}
</pre>
<p>
The same application using GtkApplication:
</p>
<pre class="programlisting">
static void
activate (GtkApplication *app)
{
  GList *list;
  GtkWidget *window;

  list = gtk_application_get_windows (app);

  if (list)
    {
      gtk_window_present (GTK_WINDOW (list-&gt;data));
    }
  else
    {
      window = create_my_window ();
      gtk_window_set_application (GTK_WINDOW (window), app);
      gtk_widget_show (window);
    }
}

int
main (int argc, char *argv[])
{
  GtkApplication *app;
  gint status;

  app = gtk_application_new ("org.gtk.TestApplication", 0);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);

  status = g_application_run (app);

  g_object_unref (app);

  return status;
}
</pre>
<p>
</p>
</div>
</div>
<br class="example-break"><div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="id3296490"></a>Uniqueness</h3></div></div></div>
<p>
      Instead of creating a #UniqueApp with unique_app_new(), create
      a #GApplication with g_application_new() or a #GtkApplication
      with gtk_application_new(). The @name that was used with
      unique_app_new() is very likely usable as the @application_id for
      g_application_new() without any changes, and GtkApplication passes
      the <code class="envar">DESKTOP_STARTUP_ID</code> environment variable
      automatically.
    </p>
<p>
      While libunique expects you to check for an already running instance
      yourself and activate it manually, GApplication handles all this on
      its own in g_application_run(). If you still need to find out if there
      is a running instance of your application, use
      g_application_get_is_remote() instead of unique_app_is_running().
    </p>
</div>
</div>
<div class="footer">
<hr>
          Generated by GTK-Doc V1.18</div>
</body>
</html>